package com.xxf.androidsqlitelib;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.orhanobut.logger.Logger;
import com.xxf.androidsqlitelib.modle.StudentMsgBean;
import com.xxf.androidsqlitelib.modle.StudentMsgBeanDao;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.inster)
    TextView inster;
    @BindView(R.id.delete)
    TextView delete;
    @BindView(R.id.query)
    TextView query;
    @BindView(R.id.modify)
    TextView modify;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        sort();
    }

    //
    private void query() {
        StudentMsgBeanDao msgBeanDao = App.getInstances().getDaoSession().getStudentMsgBeanDao();
        List<StudentMsgBean> list = msgBeanDao.queryBuilder()
                .offset(1)//偏移量，相当于 SQL 语句中的 skip,(从1开始取数据,不是从0开始)
                .limit(3)//只获取结果集的前 3 个数据
                .orderAsc(StudentMsgBeanDao.Properties.StudentNum)//通过 StudentNum 这个属性进行正序排序
                .where(StudentMsgBeanDao.Properties.Name.eq("zone"))//数据筛选，只获取 Name = "zone" 的数据。
                .build()
                .list();
        Logger.d(new Gson().toJson(list));
    }

    //曾
    private void instert() {
        StudentMsgBeanDao msgBeanDao = App.getInstances().getDaoSession().getStudentMsgBeanDao();
        StudentMsgBean studentMsgBean = new StudentMsgBean();
        studentMsgBean.setName("zone");
        studentMsgBean.setStudentNum("123456");
        //曾
        msgBeanDao.insert(studentMsgBean);
        Logger.d("instert");
    }

    //删
    private void delete() {
        StudentMsgBeanDao msgBeanDao = App.getInstances().getDaoSession().getStudentMsgBeanDao();
        List<StudentMsgBean> list = msgBeanDao.queryBuilder()
                .build().list();
        for (int i = 0; i < list.size(); i++) {
            Log.d("zoneLog", "studentNumber: " + list.get(i).getStudentNum());
            Log.d("zoneLog", "name: " + list.get(i).getName());
            if (i == 0) {
                msgBeanDao.deleteByKey(list.get(0).getId());//通过 Id 来删除数据
//                        msgBeanDao.delete(list.get(0));//通过传入实体类的实例来删除数据
            }

        }
    }

    /**
     * 改
     */
    public void update() {
        StudentMsgBeanDao msgBeanDao = App.getInstances().getDaoSession().getStudentMsgBeanDao();
        StudentMsgBean mDayStep = new StudentMsgBean((long) 1, "sad", "sass");
        msgBeanDao.update(mDayStep);
    }

    /**
     * 修改或者替换（有的话就修改，没有则替换）
     */
    public void insertOrReplace(long i, String date) {
        StudentMsgBeanDao msgBeanDao = App.getInstances().getDaoSession().getStudentMsgBeanDao();
        StudentMsgBean mDayStep = new StudentMsgBean((long) i, "", "");
        msgBeanDao.insertOrReplace(mDayStep);
    }

    @OnClick({R.id.inster, R.id.delete, R.id.query, R.id.modify})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.inster:
                instert();
                break;
            case R.id.delete:
                delete();
                break;
            case R.id.query:
                query();
                break;
            case R.id.modify:
                break;
        }
    }

    //超级连排法
    //最少只重复n次 最多重复(n-1)n/2次
    public void sort() {
        int[] array = {1, 2, 3, 4, 5, 6, 7};
        int num = 0;
        for (int i = 0; i < array.length; i++) {
            int j = i;
            while (j - 1 >= 0 && array[j] < array[j - 1]) {
                swap(array, j, j - 1);
                j--;
                num++;
            }
        }
        Logger.d(num);
        Logger.d(array);

    }

    public static void swap(int[] arr, int a, int b) {
        arr[a] = arr[a] + arr[b];
        arr[b] = arr[a] - arr[b];
        arr[a] = arr[a] - arr[b];
    }
}
